package com.fate.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import com.fate.framework.exception.KKException;
import com.fate.framework.utils.CommonUtil;
import com.fate.framework.utils.SystemUtil;
import com.fate.system.dto.filepartdetail.FilePartDetailPageDTO;
import com.fate.system.entity.FilePartDetail;
import com.fate.system.mapper.FilePartDetailMapper;
import com.fate.system.service.IFilePartDetailService;
import lombok.SneakyThrows;
import org.dromara.x.file.storage.core.upload.FilePartInfo;
import org.springframework.stereotype.Service;

/**
 * @author 47349
 * @description 针对表【file_part_detail(文件分片信息表，仅在手动分片上传时使用)】的数据库操作Service实现
 * @createDate 2024-05-20 00:06:47
 */
@Service
public class FilePartDetailServiceImpl extends ServiceImpl<FilePartDetailMapper, FilePartDetail>
    implements IFilePartDetailService {

  private final ObjectMapper objectMapper = new ObjectMapper();

  public Page<FilePartDetail> getPageList(FilePartDetailPageDTO pageDTO) {
    Page<FilePartDetail> page = SystemUtil.buildPageFromDTO(pageDTO, FilePartDetail.class);
    return this.lambdaQuery().orderByDesc(FilePartDetail::getCreateTime).page(page);
  }

  /**
   * 保存文件分片信息
   *
   * @param info 文件分片信息
   */
  @SneakyThrows
  public void saveFilePart(FilePartInfo info) {
    FilePartDetail detail = toFilePartDetail(info);
    if (save(detail)) {
      info.setId(detail.getId());
    }
  }

  /** 删除文件分片信息 */
  @Override
  public void deleteFilePartByUploadId(String uploadId) {
    FilePartDetail filePartDetail =
        this.lambdaQuery().eq(FilePartDetail::getUploadId, uploadId).one();
    if (CommonUtil.isNullOrEmpty(filePartDetail)) {
      throw new KKException("文件分片信息不存在");
    }

    this.removeById(filePartDetail.getId());
  }

  /**
   * 将 FilePartInfo 转成 FilePartDetail
   *
   * @param info 文件分片信息
   */
  public FilePartDetail toFilePartDetail(FilePartInfo info) throws JsonProcessingException {
    FilePartDetail detail = new FilePartDetail();
    detail.setPlatform(info.getPlatform());
    detail.setUploadId(info.getUploadId());
    detail.setETag(info.getETag());
    detail.setPartNumber(info.getPartNumber());
    detail.setPartSize(info.getPartSize());
    detail.setHashInfo(valueToJson(info.getHashInfo()));
    Date createTime = info.getCreateTime();
    // createTime转LocalDateTime
    Instant instant = createTime.toInstant();
    // Step 2: Convert Instant to LocalDateTime using system default timezone
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

    detail.setCreateTime(localDateTime);
    return detail;
  }

  /** 将指定值转换成 json 字符串 */
  public String valueToJson(Object value) throws JsonProcessingException {
    if (value == null) return null;
    return objectMapper.writeValueAsString(value);
  }
}
